package com.example.alaporres;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

import com.jac.alapores.domain.Alapores;

public class ArmyActivity extends Activity {
	
	private Long pTambayId = 1L;
	private Long pBasaboleroId = 1L;
	private Long pBarkerId = 1L;
	
	ArrayList<Alapores> alaporesList;
	private AlaporresCustomAdapter adapter;
	ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_army);
		//alapores.set
		alaporesList = new ArrayList <Alapores>();
		
		Alapores pope = new Alapores();
		pope.setName("Pope");
		
		Alapores jan = new Alapores();
		jan.setName("Jan");
		
		Alapores angelo = new Alapores();
		angelo.setName("Angelo");
		
		alaporesList.add(pope);
		alaporesList.add(jan);
		alaporesList.add(angelo);
		
//		alaporesList.add(getTambayDetails());
//		alaporesList.add(getTambayDetails());
//		alaporesList.add(getTambayDetails());
//		alaporesList.add(getTambayDetails());
//		alaporesList.add(getBarkerDetails());
//		alaporesList.add(getBarkerDetails());
//		alaporesList.add(getBarkerDetails());
//		alaporesList.add(getBasaboleroDetails());
//		alaporesList.add(getBasaboleroDetails());
//		alaporesList.add(getBasaboleroDetails());
		
		listView = (ListView) findViewById(R.id.armylist);
		adapter = new AlaporresCustomAdapter(ArmyActivity.this, R.id.armylist, alaporesList);		
		listView.setAdapter(adapter);
		
//		AlaporesAdapter aAdapter = new AlaporesAdapter(this);
//		aAdapter.setData(alaporesList);
//
//		ListView listView = (ListView) findViewById(R.id.armylist);
//
//		listView.setAdapter(aAdapter);
		//alaporesList.setAdapter();

//		InputStream source = retrieveStream("http://alapores-service.appspot.com/rest/recruit/list/1");
//		Gson gson = new Gson();
//		Reader reader = new InputStreamReader(source);
//		
//		AvailableRecruits availableRecruits = gson.fromJson(reader, AvailableRecruits.class);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_army, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		//		switch (item.getItemId()) {
		//		case android.R.id.home:
		//			// This ID represents the Home or Up button. In the case of this
		//			// activity, the Up button is shown. Use NavUtils to allow users
		//			// to navigate up one level in the application structure. For
		//			// more details, see the Navigation pattern on Android Design:
		//			//
		//			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
		//			//
		//			NavUtils.navigateUpFromSameTask(this);
		//			return true;
		//		}
		return super.onOptionsItemSelected(item);
	}
	
	private Alapores getTambayDetails(){
		Alapores alapores = new Alapores();
		alapores.setArmor(5);
		alapores.setName("Tambay");
		alapores.setAttackPoints(2);
		alapores.setEnergyNeeded(1);
		alapores.setHitPoints(10);
		alapores.setID(pTambayId);
		//pTambayId++;
		return alapores;
	}
	
	private Alapores getBasaboleroDetails(){
		Alapores alapores = new Alapores();
		alapores.setArmor(5);
		alapores.setName("Basabolero");
		alapores.setAttackPoints(8);
		alapores.setEnergyNeeded(2);
		alapores.setHitPoints(15);
		alapores.setID(pBasaboleroId);
		pBasaboleroId++;
		return alapores;
	}
	
	private Alapores getBarkerDetails(){
		Alapores alapores = new Alapores();
		alapores.setArmor(7);
		alapores.setName("Barker");
		alapores.setAttackPoints(5);
		alapores.setEnergyNeeded(2);
		alapores.setHitPoints(17);
		alapores.setID(pBarkerId);
		pBarkerId++;
		return alapores;
	}

	private InputStream retrieveStream(String url)
	{
		DefaultHttpClient client = new DefaultHttpClient(); 

		HttpGet getRequest = new HttpGet(url);

		try {

			HttpResponse getResponse = client.execute(getRequest);
			final int statusCode = getResponse.getStatusLine().getStatusCode();

			if (statusCode != HttpStatus.SC_OK) { 				
				return null;
			}

			HttpEntity getResponseEntity = getResponse.getEntity();
			return getResponseEntity.getContent();

		} 
		catch (IOException e) 
		{
			getRequest.abort();			
		}

		return null;

	}

}
